package org.smart4j.plugin.security;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.smart4j.plugin.security.exception.AuthcException;

/**
 * @author djw E-mail:***@***.com
 * @version 创建时间：2017/10/29 19:54
 */
public final class SecurityHelper {
    public static void login(String username, String password) throws AuthcException {
        Subject currentUser= SecurityUtils.getSubject();
        if (currentUser != null) {
            UsernamePasswordToken token=new UsernamePasswordToken(username,password);
            try {
                currentUser.login(token);
            }catch (AuthenticationException e){
                throw new AuthcException(e);
            }
        }
    }

    public static void logout() {
        Subject currentUser=SecurityUtils.getSubject();
        if (currentUser != null) {
            currentUser.logout();
        }
    }
}
